package com.duobaoyu.dby.supply.biz.client.resource.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.common.core.constant.SecurityConstants;
import com.duobaoyu.dby.common.core.constant.ServiceNameConstants;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.resource.admin.feign.RemoteResourceAfterSaleReasonAdminService;
import com.duobaoyu.dby.resource.common.admin.dto.config.ReasonDTO;
import com.duobaoyu.dby.resource.remote.common.to.config.AfterSaleReasonTO;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceAfterSaleReasonAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.AfterSaleReasonDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.AfterSaleReasonVO;
import com.duobaoyu.dby.supply.biz.client.resource.converter.ResourceConverter;
import com.duobaoyu.dby.supply.biz.util.RemoteResultCheckUtil;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ResourceAfterSaleReasonAdminClientImpl
 * @createTime 15:16:00 2023/10/08
 */
@Component
@RequiredArgsConstructor
public class ResourceAfterSaleReasonAdminClientImpl implements ResourceAfterSaleReasonAdminClient {

    private final ResourceConverter resourceConverter;
    private final RemoteResourceAfterSaleReasonAdminService remoteResourceAfterSaleReasonAdminService;

    @Override
    public Map<Integer, List<AfterSaleReasonVO>> getAfterSaleReasonCacheByAfterSaleTypes(List<Integer> afterSaleTypes) {
        R<Map<Integer, List<AfterSaleReasonTO>>> result = remoteResourceAfterSaleReasonAdminService.getAfterSaleReasonCacheByAfterSaleTypes(afterSaleTypes, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, afterSaleTypes);
        Map<Integer,List<AfterSaleReasonVO>> resultMap = Maps.newHashMap();
        if(MapUtils.isNotEmpty(result.getData())){
            result.getData().forEach((k,v) -> resultMap.put(k,resourceConverter.toAfterSaleReasonVoList(v)));
        }
        return resultMap;
    }

    @Override
    public boolean createAfterSaleReason(AfterSaleReasonDTO afterSaleReason) {
        ReasonDTO dto = resourceConverter.toAfterSaleReason(afterSaleReason);
        R<Boolean> result = remoteResourceAfterSaleReasonAdminService.add(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, afterSaleReason);
        return result.getData();
    }

    @Override
    public boolean updateAfterSaleReason(AfterSaleReasonDTO afterSaleReason) {
        ReasonDTO dto = resourceConverter.toAfterSaleReason(afterSaleReason);
        R<Boolean> result = remoteResourceAfterSaleReasonAdminService.update(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, afterSaleReason);
        return result.getData();
    }

    @Override
    public boolean deleteAfterSaleReason(Long id) {
        R<Boolean> result = remoteResourceAfterSaleReasonAdminService.delete(id, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, id);
        return result.getData();
    }

    @Override
    public AfterSaleReasonVO queryAfterSaleReason(Long id) {
        R<AfterSaleReasonTO> result = remoteResourceAfterSaleReasonAdminService.queryOne(id, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, id);
        return resourceConverter.toAfterSaleReason(result.getData());
    }

    @Override
    public Page<AfterSaleReasonVO> selectPage(AfterSaleReasonDTO afterSaleReason) {
        ReasonDTO dto = resourceConverter.toAfterSaleReason(afterSaleReason);
        R<Page<AfterSaleReasonTO>> result = remoteResourceAfterSaleReasonAdminService.page(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, afterSaleReason);
        return resourceConverter.toAfterSaleReasonPage(result.getData());
    }
}
